import { Request } from './../../interfaces/request.interface';
import { CreateResoruceDto } from './dto/createaResource.dto';
import { PaginationQueryDto } from './../../common/dto/paginationQuery.dto';
import { VideoService } from './video.service';
import { CreateVideoDto, UpdateVideoDto } from './dto/createVideo.dto';
import { Body, Controller, Get, Post, Query, BadRequestException, Req, Header, Options } from '@nestjs/common';
import { ApiBody, ApiOperation, ApiProperty, ApiQuery, ApiTags } from '@nestjs/swagger';
import { NotAuthorized, Roles } from 'src/decorator/roles.decorator';

@ApiTags('视频')
@Controller('video')
export class VideoController {
    constructor(private readonly videoServ: VideoService) { }

    @Post('add')
    add(@Body() dto: CreateVideoDto) {
        return this.videoServ.createOne(dto);
    }

    @Post('delete')
    remove(@Body() body: { readonly id: string }) {
        if (!body.id) {
            throw new BadRequestException('请传入id');
        }
        return this.videoServ.deleteById(body.id);
    }

    // @Roles('admin')
    @NotAuthorized()
    @Post('list')
    getList(@Body() dto: PaginationQueryDto,@Req() req:Request) {
        console.log("🚀 ~ file: video.controller.ts ~ line 29 ~ VideoController ~ getList ~ req", req.user)
        return this.videoServ.pagination(dto);
    }

    @ApiBody({
        schema: { example: { id: '' } }
    })
    @Post('detail')
    getDetail(@Body() body: { readonly id: string }) {
        if (!body.id) {
            throw new BadRequestException('请传入参数id');
        }
        return this.videoServ.queryDetail(body.id);
    }
   

    @Post('update')
    updateOne(@Body() dto: UpdateVideoDto) {
        
      
        return this.videoServ.updateById(dto.id, dto);
    }

    @ApiOperation({ summary: '添加视频资源' })
    @Post('addResource')
    addResource(@Body() dto: CreateResoruceDto) {
        this.videoServ.addResource(dto);
    }

    @ApiQuery({ name: 'videoId', required: true })
    @ApiOperation({ summary: '观看人数加一' })
    @Get('viewsAdd')
    async viewsAdd(@Query('videoId') videoId): Promise<any> {
        const one = await this.videoServ.findOneById(videoId);
        one.views = one.views + 1;
        this.videoServ.updateById(videoId, { ...one });
    }
}
 